import 'dart:io';

import 'package:flutter/material.dart';
import 'package:m2_flutter/page/AgreementPage.dart';
import 'package:m2_flutter/page/HomePage.dart';
import 'package:m2_flutter/service/CacheService.dart';
import 'package:m2_flutter/util/util.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({Key? key}) : super(key: key);
  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();
    init();
  }

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      backgroundColor: Colors.white,
    );
  }

  Future<bool> isAgreement(NavigatorState navigator) async {
    String isFirst = "${await CacheService.getString("isFirst")}";
    if (isFirst != 'false') {
      bool? res = await navigator.push(MaterialPageRoute(
          builder: (c) => const AgreementPage(
                justLook: false,
              )));
      if (res != null && res) {
        return true;
      } else {
        return false;
      }
    } else {
      return true;
    }
  }

  Future<void> init() async {
    var navigator = Navigator.of(context);
    if (await isAgreement(navigator)) {
      Navigator.pushReplacement(
          context, MaterialPageRoute(builder: (c) => const HomePage()));
    } else {
      Util.toast("您拒绝的隐私政策和服务条款，即将退出。");
      Future.delayed(const Duration(seconds: 2)).then((value) {
        exit(0);
      });
    }
  }
}
